let App = getApp(),
  wxParse = require("../../wxParse/wxParse.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    detail: {}, // 商品详情信息
    showView : false,
    type : 1,
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let _this = this;
    // 商品id
    console.log("goodsid", options);

    if(options.type == 0){
      _this.data.showView = true
    }
    _this.data.goods_id = options.goods_id;
    // 获取商品信息
    _this.getDetail();
  },

  /**
   * 获取信息
   */
  getDetail() {
    let _this = this;
    App._get('wxappHelp/findOneById/' + _this.data.goods_id, {},
      function (result) {
        console.log("data", result.data);
        // 初始化商品详情数据
        let data = _this.initDetailData(result.data);
        _this.setData(data);

      });
  },

  initDetailData(datas) {
    let _this = this;
    let data = _this.data;

    let imageArr=[] ;
    imageArr[0] = datas.content_image;
    var times = App.timeformat2(datas.create_time);    
    console.log("image", imageArr);

    let lengths =datas.content.length;
    console.log("length", lengths);
    data.detail = {
      image: imageArr,
      id:datas.id,
      title: datas.title,
      content: datas.content,
      time:times,
      length:lengths
    }

    // 富文本转码
    if (data.detail.content.length > 0) {
      wxParse.wxParse('content', 'html', data.detail.content, _this, 0);
    }
    console.log("detail", data.detail);
    return data;
  },

  addLike:function(option){
    let _this = this;
    let data = _this.data;
    console.info("like",option);
    let id = option.target.dataset.id;
    App._get('wxappHelp/like/' + id, {},
      function (result) {
        console.log("data", result.data);
        // 初始化商品详情数据
        if(result.status == 200){
          _this.data.showView = !_this.data.showView;
          console.info("showView", _this.data.showView);
          wx.showToast({
            title:"点赞成功",
            icon: 'success',
            duration: 2000
          })
        }else{
          wx.showToast({
            title: "点赞失败",
            icon: 'success',
            duration: 2000
          })
        }
        

      });
  }


})